from PIL import ImageGrab
from selenium import webdriver
from selenium.webdriver.support.select import Select

class tinyshop_fd:
    def __init__(self,wd):
        # self.wd = webdriver.Firefox()
        # self.wd.get("http:172.16.0.200/Tinyshop_v2.4/index.php")
        # self.wd.implicitly_wait(10)
        self.wd = wd
    # 登录
    def login_fd(self,username,pwd):
        self.wd.find_element_by_link_text('登录').click()
        self.wd.find_element_by_xpath('//*[@id="account"]').send_keys(username)
        self.wd.find_element_by_xpath('/html/body/div[2]/div/div/form/ul/li[2]/input').send_keys(pwd)
        self.wd.find_element_by_xpath('/html/body/div[2]/div/div/form/ul/li[4]/button').click()
    # 购物车
    def shopping_cart(self):
        self.wd.find_element_by_link_text('服装').click()
        self.wd.find_element_by_xpath('/html/body/div[3]/div[2]/div/div/div[2]/dl/dd/ul/li[7]/dl/dt/a/img').click()
        self.wd.find_element_by_xpath('/html/body/div[3]/div[2]/div[1]/div[3]/div/dl[1]/dd/ul/li[1]/span').click()
        self.wd.find_element_by_xpath('/html/body/div[3]/div[2]/div[1]/div[3]/div/dl[2]/dd/ul/li[2]/span').click()
        self.wd.find_element_by_xpath('/html/body/div[3]/div[2]/div[1]/div[3]/div/dl[3]/dd/a[2]/i').click()
        self.wd.find_element_by_xpath('/html/body/div[3]/div[2]/div[1]/div[3]/div/dl[5]/dd/a[2]/span').click()
        self.wd.find_element_by_id('shopping-cart').click()
        self.wd.find_element_by_link_text('去购物车结算').click()
    def pay_money(self,postcode,address,name,tel,img):
        self.wd.find_element_by_link_text('立即结算').click()
        # 添加地址
        self.wd.find_element_by_xpath('//*[@id="address_other"]').click()
        # 切换界面
        self.wd.switch_to.frame(self.wd.find_element_by_xpath('/html/body/div[1]/div/table/tbody/tr[2]/td[2]/div/table/tbody/tr[2]/td[2]/div/iframe'))
        Select(self.wd.find_element_by_id('province')).select_by_value('510000')
        Select(self.wd.find_element_by_id('city')).select_by_value('510100')
        Select(self.wd.find_element_by_id('county')).select_by_value('510104')
        self.wd.find_element_by_xpath('/html/body/div[2]/form/table/tbody/tr[2]/td[2]/input').send_keys(postcode)
        self.wd.find_element_by_xpath('/html/body/div[2]/form/table/tbody/tr[3]/td[2]/textarea').send_keys(address)
        self.wd.find_element_by_xpath('/html/body/div[2]/form/table/tbody/tr[4]/td[2]/input').send_keys(name)
        self.wd.find_element_by_xpath('/html/body/div[2]/form/table/tbody/tr[5]/td[2]/input').send_keys(tel)
        self.wd.find_element_by_xpath('/html/body/div[2]/form/table/tbody/tr[8]/td/input').click()
        # 切换回原界面
        self.wd.switch_to_default_content()
        im = ImageGrab.grab()
        im.save(r'C:\Users\Administrator\Desktop\caseall\pic\%s.jpg'%(img))
        try:
            self.wd.find_element_by_xpath('/html/body/div[3]/div[2]/div/form/div[6]/p/input').click()
            self.wd.find_element_by_xpath('/html/body/div[3]/div[2]/form/div[3]/p/input').click()
        except:
            pass

    def alltest(self,username,pwd,postcode,address,name,tel,img):
        self.login_fd(username,pwd)
        self.shopping_cart()
        self.pay_money(postcode,address,name,tel,img)


        # self.wd.quit()
    def __del__(self):
        self.wd.quit()